from flask import Flask, jsonify, make_response
from flask import request, redirect
from werkzeug.routing import BaseConverter
from flask import render_template, url_for
from settings.default import DefaultConfig

import json


def create_flask_app(config):
    app = Flask(__name__)
    app.config.from_object(config)

    return app


app = create_flask_app(DefaultConfig)


# @app.route('/stus/<int:id>')
# def index(id):
#     print("当前", id)
#     return "获取成功"

# 自定义参数

# class PhoneConverter(BaseConverter):
#     regex = r'1[3-9]\d{9}'
#
#
# app.url_map.converters['phone'] = PhoneConverter
#
#
# @app.route('/stus/<phone:id>')
# def index(id):
#     print("手机号是：", id)
#     return id


# 查询字符串获取
# http://127.0.0.1:5000/stus/name=zhangsan&age=12

# @app.route('/stus')
# def StuInfo():
#     name = request.args['name']
#     age = request.args.get('age')
#     print(name, age)
#     return '获取到信息'


# 获取表单数据
# @app.route('/login', methods=['POST'])
# def login():
#     name = request.form.get('username')
#     pwd = request.form.get('password')
#     print(name, pwd)
#     return "登陆成功"


# 文件上传
# @app.route('/files', methods=['POST'])
# def postfiles():
#     f_name = request.files.get('name')
#     print("当前的文件名是>>", f_name.filename)
#
#     return "文件获取成功"


# 其他参数

# @app.route('/index')
# def index():
#     print(request)
#     print("当前的请求方式", request.method)
#     print("当前的请求路由", request.url)
#     print("当前的请求值", request.values)  # 返回的是CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([])])
#     print("当前的请求数据", request.data)  # 返回的数据类型是byte字节类型
#     print("headers", request.headers)
#     return '其他参数'


# 模板响应
# @app.route('/index', endpoint='idx')
# def index():
#     return render_template('index.html')
#
#
# # 重定向
# @app.route('/login', methods=['POST', 'GET'])
# def login():
#     name = request.args.get('username')
#     print(name)
#     return redirect(url_for('idx'))


# 响应json 数据

# @app.route('/users', methods=['POST'])
# def user_info():
#     data_ = {
#         "id": id,
#         'name': "刘君统",
#         'address': 'bzd'
#     }
#
#     # 响应json 数据
#     # return json.dumps(data_)
#     # return jsonify(id=id, name="刘君统啊")
#     # return jsonify(data_)
#
#     # 响应元组(response, status, headers)
#     # response 是响应的数据
#     # status 是响应的状态码，会覆盖默认响应的状态码
#     # headers 字典类型， 会在浏览器的响应头中出现
#     # return '刘浩瀚', '666', {'auth': 'china'}
#
#     # 自定义响应
#     file = request.files.get('name')
#     img_name = file.filename
#     # 创建响应对象
#     resp = make_response(img_name)
#     # 指定响应数据的类型
#     resp.headers['Content-Type'] = 'image/jpg'
#     # 指定响应的状态码
#     resp.status = '204'
#     return resp


if __name__ == '__main__':
    app.run()

